<template>
  <div class="adminContainer">
    <div class="search">
      <el-input v-model.trim="searchValue" placeholder="请输入要查询的管理员名字或者电话" clearable :prefix-icon="Search" @keyup="getAdminList(searchValue)">
        <template #append>
          <el-button :icon="Search" @click="getAdminList(searchValue)"/>
        </template>
      </el-input>
      <el-button style="margin-left: 30px;" type="primary" @click="addAdminVisable = true">新建管理员</el-button>
      <el-dialog v-model="addAdminVisable" title="添加管理员" width="30%" draggable>
        <el-form v-model="addForm">
          <el-form-item prop="管理员名称">
            <el-input v-model="addForm.adminName" placeholder="请输入管理员姓名" :prefix-icon="User"/>
          </el-form-item>
          <el-form-item prop="初始密码">
            <el-input v-model="addForm.password" placeholder="请输入初始密码" :prefix-icon="Lock"/>
          </el-form-item>
          <el-form-item prop="联系电话">
            <el-input v-model="addForm.tel" placeholder="请输入管理员电话" :prefix-icon="Iphone"/>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="addAdminVisable = false">取消</el-button>
            <el-button type="primary" @click="addAdmin()">确认</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
    <el-table :data="tableData" stripe style="width: 100%;margin-top: 10px;">
    <el-table-column prop="adminId" label="管理员ID" />
    <el-table-column prop="adminName" label="管理员名字" />
    <el-table-column prop="roleName" label="角色" :filters="[
        { text: '超级管理员', value: '超级管理员' },
        { text: '管理员', value: '管理员' },]" :filter-method="filterName"  filter-placement="bottom-end">
    </el-table-column>
    <el-table-column label="密码" >
      <template #default="row">
        {{commonUtil.encodeRSA(row.row.password, null)}}
      </template>
    </el-table-column>
    <el-table-column prop="tel" label="联系电话" />
    <el-table-column prop="univerName" label="学校" />
    <el-table-column prop="collegeName" label="学院" />
    <el-table-column prop="detail" label="详细信息" />
    <el-table-column fixed="right" label="Operations" width="120">
      <template #default="row">
        <el-tooltip content="详情" placement="bottom">
          <el-button link type="primary" :icon="Postcard"  class="myLink"  @click="getDetailMessage(row)"></el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="bottom">
          <el-button link type="danger" :icon="Delete"  class="myLink" @click="deleteAdmin(index,row)"></el-button>
        </el-tooltip>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="detailMessage" title="详情" width="30%" draggable>
    <el-form v-model="detailAdmin" label-position="left">
      <el-form-item prop="adminId" label="管&nbsp;理&nbsp;员&nbsp;ID: ">
        <span>{{detailAdmin.adminId}} </span>
      </el-form-item>
      <el-form-item prop="adminName" label="名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;字: ">
        <span>{{detailAdmin.adminName}} </span>
      </el-form-item>
      <el-form-item prop="cdId"  width="100" label="学院详细ID: ">
        <span>{{detailAdmin.cdId}} </span>
      </el-form-item>
      <el-form-item prop="detail"  width="100" label="学&nbsp;院&nbsp;详&nbsp;细: ">
        <span>{{detailAdmin.detail}} </span>
      </el-form-item>
      <el-form-item prop="collegeId"  width="100" label="学&nbsp;&nbsp;院&nbsp;&nbsp;&nbsp;ID: ">
        <span>{{detailAdmin.collegeId}} </span>
      </el-form-item>
      <el-form-item prop="collegeName"  width="100" label="学&nbsp;院&nbsp;名&nbsp;称: ">
        <span>{{detailAdmin.collegeName}} </span>
      </el-form-item>
      <el-form-item prop="password"  width="100" label="密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码: ">
        <span>{{detailAdmin.password}} </span>
      </el-form-item>
      <el-form-item prop="tel"  width="100" label="联&nbsp;系&nbsp;电&nbsp;话: ">
        <span>{{detailAdmin.tel}} </span>
      </el-form-item>
      <el-form-item prop="univerName"  width="100" label="学&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;校: ">
        <span>{{detailAdmin.univerName}} </span>
      </el-form-item>
      <el-form-item prop="imagUrl"  width="100" label="头&nbsp;像&nbsp;图&nbsp;片: ">
        <img :src="detailAdmin.imagUrl" style="width: 50px;height: 50px;" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="detailMessage = false">取消</el-button>
      </span>
    </template>
  </el-dialog>
  <div class="pagination">
    <el-pagination v-model:current-page="page" v-model:page-size="size" :page-sizes="[5,10,20,30]" :small="small" :disabled="disabled"
      :background="background" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </div> 
  </div>
</template>

<script setup>
import { reactive , inject , onMounted , ref } from "vue"
import axios from '../../axios/axiosInstance'
import commonUtil from '../../utils/commonUtil.js'
import { ElLoading ,ElMessage,ElMessageBox} from 'element-plus'
import { Search ,User,Lock,Iphone,Delete,Postcard} from '@element-plus/icons-vue'



const addAdminVisable = ref(false)
let addForm = reactive({
  adminName: "",
  tel: "",
  password: "",
})
let detailMessage = ref(false)
let tableData = reactive([])
const total = ref(0)
let searchValue = ref()
let page = 1
let size = 5
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const detailAdmin= reactive({
  adminId: 10001,
  adminName: "super_admin",
  cdId: 5,   
  collegeId: 1,
  collegeName: "计算机科学与技术学院", 
  detail: "教务处",
  imagUrl: "http://pic.pptbz.com/pptpic/201306/2013062320262198_S.jpg",
  password: "111111",
  tel: "16684867746",
  univerName: "贵州大学",
})


const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  size = val
  init()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  page = val
  init()
}
const getDetailMessage = (row) => {
  detailMessage.value = true;
  detailAdmin.adminId = row.row.adminId,
  detailAdmin.adminName = row.row.adminName,
  detailAdmin.cdId = row.row.cdId,
  detailAdmin.collegeId = row.row.collegeId,
  detailAdmin.collegeName = row.row.collegeName,
  detailAdmin.detail = row.row.detail,
  detailAdmin.imagUrl = row.row.imagUrl,
  detailAdmin.password = row.row.password,
  detailAdmin.tel = row.row.tel,
  detailAdmin.univerName = row.row.univerName
}
const filterName = (value, row) => {
  return row.roleName === value
}


 const clear = (value) => {
    Object.keys(value).map(key => {
      delete value[key]
    })
  }
//方法
const addAdmin = () => {
  //将数据发往后端
  axios.post('/account-service/admin/addAdmin',addForm).then((res) => {   
        console.log("res是：")
        console.log(res)
        console.log("res.data是：")
        console.log(res.data)
        console.log("res.data.data是：")
        console.log(res.data)
        if(res.code == 200){
          //添加管理员成功成功
          ElMessage({type: "success",message: "添加成功"});
          tableData.push(res.data);
          addAdminVisable = false;
        }else{
          //添加失败
         ElMessage({type: "error",message: res.msg})
         addAdminVisable = false;
        }
    }).catch((err) => {
      console.log(err)

    })
}


// 删除用户信息
const deleteAdmin = (index,row) => {
  console.log(row);
  console.log(row.row);
  console.log(index)
  console.log(row.$index)
  console.log(row.row.adminId);
  let name = row.row.adminName
  ElMessageBox.confirm(
    '确定要删除管理员'+name+'吗？',
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  )
    .then(() => {
      let adminId = row.row.adminId;
      console.log(adminId)
      axios.delete('/account-service/admin/'+adminId).then((res) => {
        console.log(res)
        if(res.code == 200){
          ElMessage({type: "success",message: "删除成功"});
          tableData.pop(row.$index)
        }else{
          ElMessage({type: "error",message: "删除失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '删除取消',
      })
    }) 
}

//根据搜索关键字进行模糊查询管理员
const getAdminList = (searchValue) => {
  if(searchValue != ''){
    axios.get(`/account-service/admin/adminList/${searchValue}`).then((res) => {
        console.log(res)
        
        if(res.code == 200){
          clear(tableData)
          for(let i in res.data){
            console.log(i)
            tableData.push(res.data[i])
          }
          ElMessage({type: "success",message: "查询成功"});
        }else{
          ElMessage({type: "error",message: "查询失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
  }else{
    init()
  }
}

const init = async() => {
  const loading = ElLoading.service({
        lock: true,
        text: '拼命加载中',
        background: 'rgba(0, 0, 0, 0.7)',
      })
      setTimeout(() => {
        loading.close()
      }, 2000)

    try{
        const response = await axios.get(`/account-service/admin/adminList/${page}/${size}`)
        console.log(response)
        clear(tableData)
        // total.value = response.data.total
        for(let i in response.data.content){
        console.log(i)
        tableData.push(response.data.content[i])
      }
      console.log("最后的tableData是：")
      console.log(tableData)
     } catch (error) {
    console.error(error);
     }
}

//页面初始化方法
onMounted(()=>{ 
  init()
})
</script>

<style scoped>
.adminContainer .search{
  display: flex;
  width: 600px;
}

.adminContainer .pagination{
 justify-content: center;
 margin-top: 15px;
}
</style>
